//
//  ZYDeviceSwitchView.m
//  YiLin
//
//  Created by Zyark on 2018/6/3.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYDeviceSwitchView.h"
#import "ZYImageUtils.h"

#define WIND_SPEED_BTN_WIDHT 45

@interface ZYDeviceSwitchView()
@property(nonatomic,strong) UIView *windSpeedView;
@property(nonatomic,strong) UIView *switchView;
@property(nonatomic,strong) UILabel *windSpeedLabel;
@property(nonatomic,strong) UILabel *deviceSwitchLabel;
@property(nonatomic,strong) UILabel *lockDeviceLabel;
/**是否开启*/
@property(nonatomic,strong) UISwitch *openSwitch;
/**是否锁定*/
@property(nonatomic,strong) UISwitch *lockSwitch;
/**风速自动*/
@property(nonatomic,strong) UIButton *autoBtn;
/**风速高*/
@property(nonatomic,strong) UIButton *heightBtn;
/**风速中*/
@property(nonatomic,strong) UIButton *middleBtn;
/**风速低*/
@property(nonatomic,strong) UIButton *lowBtn;

@property(nonatomic,strong) ZYChangeLanguage *language;

@property(nonatomic,strong) MASConstraint *autoBtnWidth;

@property(nonatomic,strong) MASConstraint *windSpeedViewHeight;
@end

@implementation ZYDeviceSwitchView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        _language = [ZYChangeLanguage sharedZYChangeLanguage];
        [self setup];
    }
    return self;
}

-(void)setup{
    UIView *windSpeedView = [[UIView alloc] init];
    UIView *switchView = [[UIView alloc] init];
    UILabel *windSpeedLabel = [self setupLabel];
    UILabel *deviceSwitchLabel = [self setupLabel];
    UILabel *lockDeviceLabel = [self setupLabel];
    UISwitch *openSwitch = [self setupSwitch];
    UISwitch *lockSwitch = [self setupSwitch];
    UIButton *autoBtn = [self setupButton];
    UIButton *heightBtn = [self setupButton];
    UIButton *middleBtn = [self setupButton];
    UIButton *lowBtn = [self setupButton];
    
    [self addSubview:windSpeedView];
    [self addSubview:switchView];
    
    [switchView addSubview:deviceSwitchLabel];
    [switchView addSubview:lockDeviceLabel];
    [switchView addSubview:openSwitch];
    [switchView addSubview:lockSwitch];
    
    [windSpeedView addSubview:windSpeedLabel];
    [windSpeedView addSubview:autoBtn];
    [windSpeedView addSubview:heightBtn];
    [windSpeedView addSubview:middleBtn];
    [windSpeedView addSubview:lowBtn];
    
    ZYWeakSelf(weakSelf)
    CGFloat left = 15;
    CGFloat margin = 10;
    CGFloat windSpeedBtnMargin = 17;//风速按钮间距
    CGFloat labelHeight = 35;
    CGFloat width = WIND_SPEED_BTN_WIDHT;
    if(ZYiPhone5_OR_5c_OR_5s || ZYiPhone4_OR_4s){
        left = 12;
        margin = 8;
        windSpeedBtnMargin = 12;
        labelHeight = 35;
    }else if(ZYiPhone6Plus_OR_6sPlus){
        windSpeedBtnMargin = 20;
        width = 55;
    }
    
    [windSpeedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.equalTo(weakSelf.mas_left);
        make.right.equalTo(weakSelf.mas_right);
        _windSpeedViewHeight = make.height.mas_equalTo(DEVICE_SWITCH_VIEW_COMMON_HEIGHT / 2);
    }];
    
    [switchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left);
        make.right.equalTo(weakSelf.mas_right);
        make.top.equalTo(windSpeedView.mas_bottom);
        make.height.mas_equalTo(DEVICE_SWITCH_VIEW_COMMON_HEIGHT / 2);
    }];
    
    [windSpeedLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(left);
        make.centerY.mas_equalTo(windSpeedView);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(labelHeight);
    }];
    
    [autoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        _autoBtnWidth = make.width.and.height.mas_equalTo(width);
        make.centerY.mas_equalTo(windSpeedView);
        make.left.equalTo(windSpeedLabel.mas_right).offset(margin);
    }];
    
    [heightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.and.height.mas_equalTo(width);
        make.centerY.mas_equalTo(windSpeedView);
        make.left.equalTo(autoBtn.mas_right).offset(windSpeedBtnMargin);
    }];
    
    [middleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.and.height.mas_equalTo(width);
        make.centerY.mas_equalTo(windSpeedView);
        make.left.equalTo(heightBtn.mas_right).offset(windSpeedBtnMargin);
    }];
    
    [lowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.and.height.mas_equalTo(width);
        make.centerY.mas_equalTo(windSpeedView);
        make.left.equalTo(middleBtn.mas_right).offset(windSpeedBtnMargin);
    }];
    
    [deviceSwitchLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(left);
        make.centerY.mas_equalTo(switchView);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(labelHeight);
    }];
    
    [openSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(deviceSwitchLabel.mas_right).offset(margin);
        make.centerY.mas_equalTo(switchView);
    }];
    
    [lockDeviceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(switchView.mas_centerX).offset(left);
        make.centerY.mas_equalTo(switchView);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(labelHeight);
    }];
    
    [lockSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lockDeviceLabel.mas_right).offset(margin);
        make.centerY.mas_equalTo(switchView);
    }];
    
    self.backgroundColor = RGBA(248, 248, 248, 1);
    self.layer.cornerRadius = 5;
    self.layer.masksToBounds = YES;
    
    _windSpeedView = windSpeedView;
    _switchView = switchView;
    _windSpeedLabel = windSpeedLabel;
    _deviceSwitchLabel = deviceSwitchLabel;
    _lockDeviceLabel = lockDeviceLabel;
    _openSwitch = openSwitch;
    _lockSwitch = lockSwitch;
    _autoBtn = autoBtn;
    _heightBtn = heightBtn;
    _middleBtn = middleBtn;
    _lowBtn = lowBtn;
    
    _autoBtn.tag = ZYWindSpeedTypeAuto;
    _heightBtn.tag = ZYWindSpeedTypeHeight;
    _middleBtn.tag = ZYWindSpeedTypeMiddle;
    _lowBtn.tag = ZYWindSpeedTypeLow;
    
    [_openSwitch addTarget:self action:@selector(doOpen:) forControlEvents:UIControlEventValueChanged];
    [_lockSwitch addTarget:self action:@selector(doLock:) forControlEvents:UIControlEventValueChanged];
    
    [self setupTitle];
}

-(void)setupTitle{
    _windSpeedLabel.text = [_language getStringByKey:LC_HOME_WIND_SPEED_LABEL];
    _deviceSwitchLabel.text = [_language getStringByKey:LC_HOME_DEVICE_SWITCH_LABEL];
    _lockDeviceLabel.text = [_language getStringByKey:LC_HOME_LOCK_THE_DEVICE_LABEL];
    
    [_autoBtn setTitle:[_language getStringByKey:LC_HOME_WIND_SPEED_TYPE_AUTO] forState:UIControlStateNormal];
    [_autoBtn setTitle:[_language getStringByKey:LC_HOME_WIND_SPEED_TYPE_AUTO] forState:UIControlStateSelected];
    
    [_heightBtn setTitle:[_language getStringByKey:LC_HOME_WIND_SPEED_TYPE_HEIGHT] forState:UIControlStateNormal];
    [_heightBtn setTitle:[_language getStringByKey:LC_HOME_WIND_SPEED_TYPE_HEIGHT] forState:UIControlStateSelected];
    
    [_middleBtn setTitle:[_language getStringByKey:LC_HOME_WIND_SPEED_TYPE_MIDDLE] forState:UIControlStateNormal];
    [_middleBtn setTitle:[_language getStringByKey:LC_HOME_WIND_SPEED_TYPE_MIDDLE] forState:UIControlStateSelected];
    
    [_lowBtn setTitle:[_language getStringByKey:LC_HOME_WIND_SPEED_TYPE_LOW] forState:UIControlStateNormal];
    [_lowBtn setTitle:[_language getStringByKey:LC_HOME_WIND_SPEED_TYPE_LOW] forState:UIControlStateSelected];
}

-(UIButton *)setupButton{
    CGFloat width = 45;
    if(ZYiPhone6Plus_OR_6sPlus){
        width = 55;
    }
    UIButton *button = [[UIButton alloc] init];
    button.size = CGSizeMake(width, width);
    [button setBackgroundImage:[ZYImageUtils imageWithColor:kMainColor rect:button.frame] forState:UIControlStateSelected];
    [button setBackgroundImage:[ZYImageUtils imageWithColor:[UIColor whiteColor] rect:button.frame] forState:UIControlStateNormal];
    [button setTitleColor:kMainColor forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [button.titleLabel setFont:[UIFont systemFontOfSize:14]];
    button.layer.cornerRadius = button.height / 2;
    button.layer.masksToBounds = YES;
    button.layer.borderWidth = 1;
    button.layer.borderColor = kMainColor.CGColor;
    [button addTarget:self action:@selector(windSpeedSelect:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

-(UISwitch *)setupSwitch{
    UISwitch *switchBtn = [[UISwitch alloc] init];
    [switchBtn setOnTintColor:kMainColor];
    return switchBtn;
}

-(UILabel *)setupLabel{
    UILabel *label = [[UILabel alloc] init];
    label.textColor = kLabelColor;
    label.font = [UIFont systemFontOfSize:16];
    label.height = 30;
    return label;
}

#pragma mark -Event
-(void)windSpeedSelect:(UIButton *)button{
    if(_device.isLock || !_device.isOpen){
        return;
    }
    [self doWindSpeedSelect:button];
    if([_delegate respondsToSelector:@selector(deviceSwitchView:setWindSpeedType:device:button:)]){
        [_delegate deviceSwitchView:self setWindSpeedType:button.tag device:_device button:button];
    }
}

-(void)doWindSpeedSelect:(UIButton *)button{
    if(button.isSelected){
        return;
    }
    [self clearSelectedButton:_autoBtn];
    [self clearSelectedButton:_heightBtn];
    [self clearSelectedButton:_middleBtn];
    [self clearSelectedButton:_lowBtn];
    button.selected = YES;
    _device.windSpeedType = button.tag;
}


-(void)clearSelectedButton:(UIButton *)button{
    button.selected = NO;
}

-(void)doOpen:(UISwitch *)switchBtn{
    if(_device.isLock){
        return;
    }
    _device.isOpen = switchBtn.on;
    [self switchWindSpeed];
    if([_delegate respondsToSelector:@selector(deviceSwitchView:setDeviceOpen:device:switchBtn:)]){
        [_delegate deviceSwitchView:self setDeviceOpen:switchBtn.on device:_device switchBtn:switchBtn];
    }
}

-(void)doLock:(UISwitch *)switchBtn{
    _device.isLock = switchBtn.on;
    _openSwitch.enabled = !switchBtn.on;
    if([_delegate respondsToSelector:@selector(deviceSwitchView:setDeviceLock:device:switchBtn:)]){
        [_delegate deviceSwitchView:self setDeviceLock:switchBtn.on device:_device switchBtn:switchBtn];
    }
}

-(void)switchWindSpeed{
    _autoBtn.selected = NO;
    _heightBtn.selected = NO;
    _middleBtn.selected = NO;
    _lowBtn.selected = NO;
    if(_device.isOpen){
        switch (_device.windSpeedType) {
            case ZYWindSpeedTypeAuto:
                [self doWindSpeedSelect:_autoBtn];
                break;
            case ZYWindSpeedTypeHeight:
                [self doWindSpeedSelect:_heightBtn];
                break;
            case ZYWindSpeedTypeMiddle:
                [self doWindSpeedSelect:_middleBtn];
                break;
            case ZYWindSpeedTypeLow:
                [self doWindSpeedSelect:_lowBtn];
                break;
            default:
                break;
        }
    }
}

-(void)setDevice:(ZYDevice *)device{
    _device = device;
    [self switchWindSpeed];
    _device.isLock = device.isLock;
    _lockSwitch.on = device.isLock;
    _openSwitch.on = device.isOpen;
    _openSwitch.enabled = !device.isLock;
    if(_device.type == ZYDeviceTypeVentilationSystem){
        //新风系统
        _windSpeedView.hidden = NO;
        [_autoBtnWidth uninstall];
        [_autoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            _autoBtnWidth = make.width.and.height.mas_equalTo(0);
        }];
        [_windSpeedViewHeight uninstall];
        [_windSpeedView mas_makeConstraints:^(MASConstraintMaker *make) {
            _windSpeedViewHeight = make.height.mas_equalTo(DEVICE_SWITCH_VIEW_COMMON_HEIGHT/2);
        }];
        self.height = DEVICE_SWITCH_VIEW_COMMON_HEIGHT;
    }else if(_device.type == ZYDeviceTypeRadiantFloorHeating){
        //地暖
        _windSpeedView.hidden = YES;
        [_windSpeedViewHeight uninstall];
        [_windSpeedView mas_makeConstraints:^(MASConstraintMaker *make) {
            _windSpeedViewHeight = make.height.mas_equalTo(0);
        }];
        self.height = DEVICE_SWITCH_VIEW_RFH_HEIGHT;
    }else if(_device.type == ZYDeviceTypeAirConditioning){
        //空调
        _windSpeedView.hidden = NO;
        [_autoBtnWidth uninstall];
        [_autoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            CGFloat width = WIND_SPEED_BTN_WIDHT;
            if(ZYiPhone6Plus_OR_6sPlus){
                width = 55;
            }
            _autoBtnWidth = make.width.and.height.mas_equalTo(width);
        }];
        [_windSpeedViewHeight uninstall];
        [_windSpeedView mas_makeConstraints:^(MASConstraintMaker *make) {
            _windSpeedViewHeight = make.height.mas_equalTo(DEVICE_SWITCH_VIEW_COMMON_HEIGHT/2);
        }];
        self.height = DEVICE_SWITCH_VIEW_COMMON_HEIGHT;
    }
    
//     调用此方法告诉self.view检测是否需要更新约束，若需要则更新，下面添加动画效果才起作用
    [self updateConstraintsIfNeeded];
    [UIView animateWithDuration:0.3 animations:^{
        [self layoutIfNeeded];
    }];
}

@end
